//
//  PersonViewController.m
//  SDSY
//
//  Created by dale on 2017/3/23.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "PersonViewController.h"
#import "PersonView.h"
#import "ModifyPwdTableViewController.h"
#import "UserModel.h"
#import "UserInfoModel.h"

@interface PersonViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation PersonViewController{
    UITableView*    _tableView;
    PersonView*     _personHeader;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人设置";
    self.view.backgroundColor = [UIColor yellowColor];
    
    _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    _personHeader=[[PersonView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 110)];
    // 取出UserModel
    UserModel *model = [UserInfoModel shareInstance].userModel;
    if (model != nil) {
        // 设置视图
        _personHeader.iconStr = model.picture_url;
        _personHeader.majorStr = model.major;
        _personHeader.nameStr = model.name;
        [_personHeader setupView];
    }
    [_personHeader setupView];
    [_personHeader setNoTapped];//取消点击手势隐藏箭头
    _tableView.tableHeaderView=_personHeader;
    _tableView.sectionHeaderHeight=0;
    [self.view addSubview:_tableView];
    
}

//设置cell属性
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"id"];
    }
    if(indexPath.section==0){
//        if(indexPath.row==0){
//            cell.textLabel.text=@"修改头像";
//        }else if(indexPath.row==1){
//            cell.textLabel.text=@"修改密码";
//        }else if (indexPath.row==2){
//            cell.textLabel.text=@"绑定/修改手机";
//        }
        if(indexPath.row==0){
            cell.textLabel.text=@"修改密码";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
    }
    
    return cell;
}

//设置section标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"基本设置";
    }
    return @"";
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 38;
}
//取消尾部视图的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}


//划分单元
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//划分每个单元的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //设置tableViewCell选中风格
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section==0) {
        
        if (indexPath.row==0){
            NSLog(@"修改密码");
            ModifyPwdTableViewController* modifyPwdVC=[[ModifyPwdTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
            modifyPwdVC.hidesBottomBarWhenPushed=YES;//隐藏下方BottomBar
            [self.navigationController pushViewController:modifyPwdVC animated:YES];
         
        }
    }
}

#pragma mark - 自定义方法
-(void)pickImage:(UIImagePickerControllerSourceType) type {
    // 1.检查设备是否支持这种方式
    if([UIImagePickerController isSourceTypeAvailable:type]){
        // 设置imagePickerController
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.sourceType = type;
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = true;
        [self presentViewController:imagePickerController animated:true completion:nil];
    } else {
        // 如果获取不到设备
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"⚠️ 警告"
                                                                                 message:@"摄像头会相簿不可用!"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:action];
        [self presentViewController:alertController animated:true completion:nil];
        return;
    }

}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
}



@end
